package com.sh.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import sh.dto.ResultDto;
import sh.util.JwtUtil;

import java.util.HashMap;
import java.util.List;

@Component
@Slf4j
public class AuthGlobalFilter implements GlobalFilter, Ordered {
    @SneakyThrows
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        HttpHeaders headers = exchange.getRequest().getHeaders();
        ServerHttpRequest request = exchange.getRequest();
        RequestPath path = request.getPath();

        // 获取请求路径
        PathContainer pathContainer = path.pathWithinApplication();
        PathContainer pathContainer1 = pathContainer.subPath(1);
        String value = pathContainer1.value();

        List<String> authorization = headers.get("Authorization");
        if (authorization==null|| StringUtils.isEmpty(authorization.get(0))) {
            ServerHttpResponse response = exchange.getResponse();
            log.info("token不能为空");
            ResultDto resultDto = new ResultDto(1001, "token不能为空");
            ObjectMapper objectMapper = new ObjectMapper();
            String res = objectMapper.writeValueAsString(resultDto);
            response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
          response.setStatusCode(HttpStatus.UNAUTHORIZED);
            DataBuffer wrap = response.bufferFactory().wrap(res.getBytes());
            Mono<Void> voidMono = response.writeWith(Mono.just(wrap));
            return voidMono;
        }

        Integer userId = JwtUtil.getUserId(authorization.get(0));
        if (!JwtUtil.verfy(authorization.get(0),userId)) {

        }
        HashMap<String,Object> data = JwtUtil.getData(authorization.get(0), HashMap.class);
        Boolean isAdmin = (Boolean) data.get("isAdmin");
        if (isAdmin) {
            return chain.filter(exchange);
        }
        /*List<Auth> authList = (List<Auth>) data.get("authList");
        //不是的话就根据权限进行对应
        for (Auth auth : authList) {
            if(auth.getAuthUrl()==null || auth.getAuthUrl()==""){
                continue;
            }
            if (servletPath.equals(auth.getAuthUrl())) {
                return chain.filter(exchange);
            }
        }*/
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
